
var hogan = require('hogan');

var config = {
    serverHost : 'http://localhost:8080',
    frontHost  : 'http://localhost:8888/dist/view/'
}

var _common_util = {

    request : function(param){
        var _this = this;
        $.ajax({
            type    :   param.method || 'get',
            url     :   param.url    || '',
            dataType:   param.type   || 'json',
            data    :   param.data   || '',
            success :   function(res){
                if( 0=== res.status ){
                    typeof param.success === 'function' && param.success(res.data, res.msg);
                }
                /* 需要登录 */
                else if (10 == res.status){
                    this.dologin();
                }
                else if(1 == res.status){
                    typeof param.error === 'function' && param.error(res.msg);
                }
            },
            error   :   function(err){
                typeof param.error === 'function' && param.error(err.statusText);
            }
        });
    },
    doLogin : function(){
        var _this = this;
        // window.location.href = './user-login.html?redirect=' + encodeURIComponent(window.location.href);
        window.location.href = this.getFrontURL('user-login.html?redirect=') + encodeURIComponent(window.location.href);
    },
    getServerURL : function(path){
        return config.serverHost + path;
    },
    getFrontURL :   function(path){
        return config.frontHost + path;
    },
    getURLParam :function(name){
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
        var result  = window.location.search.substr(1).match(reg);
        return result ? decodeURIComponent(result[2]) : null;
    },
    renderHTML : function(htmlTemplate, data){
        var template    = hogan.compile(htmlTemplate);
        var result      = template.render(data);
        return result;
    },
     // 成功提示
    successTips : function(msg){
        alert(msg || '操作成功！');
    },
    // 错误提示
    errorTips : function(msg){
        alert(msg || '哪里不对了~');
    },
    // 字段的验证，支持非空、手机、邮箱的判断
    validate : function(value, type){
        var value = $.trim(value);
        // 非空验证
        if('require' === type){
            return !!value;
        }
        // 手机号验证
        if('phone' === type){
            return /^1\d{10}$/.test(value);
        }
        // 邮箱格式验证
        if('email' === type){
            return /^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/.test(value);
        }
    },
    goHome : function(){
        var _this = this;
        // window.location.href = './index.html';
        window.location.href = _this.getFrontURL('');
    },
    setCookie : function(c_name, value, expiresdays)
    {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiresdays);
        //对value值进行编码
        document.cookie = c_name + "=" + escape(value)+
        ((expiresdays==null) ? "" : ";expires="+exdate.toGMTString());
    },
    getCookie : function(c_name)
    {
        if(document.cookie.length>0){
            c_start = document.cookie.indexOf(c_name + "=");
            if(c_start!=-1){ 
                c_start = c_start + c_name.length+1;
                c_end = document.cookie.indexOf(";",c_start);
                if(c_end==-1) 
                    c_end = document.cookie.length;
                    //解码
                    return unescape(document.cookie.substring(c_start,c_end));
            } 
        }
        return "";
    },
    delCookie   : function(c_name){
        document.cookie = c_name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
    }

};

module.exports = _common_util;